#include<unordered_map>
#include<algorithm>
using std::max;
using std::unordered_map;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

struct TreeSelect
{
    int select;
    int notSelect;
};

class Solution {
public:
    int rob(TreeNode* root) {
        if (!root) return 0;
        auto node = dfs(root);
        return max(node.notSelect, node.select);
    }

    TreeSelect dfs(TreeNode* root) {
        if (!root) return {0, 0};
        TreeSelect node;
        auto l = dfs(root->left);
        auto r = dfs(root->right);
        node.select = root->val + l.notSelect + r.notSelect;
        node.notSelect = max(l.select, l.notSelect) + max(r.select, r.notSelect);
        return node;

    }
};